/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.MembershipManager;

import java.util.Random;
import java.util.regex.Pattern;
import resource.Macro;
import resource.Member;
import utilities.DBConnector;
import utilities.Codecs;
import utilities.Helper;
import utilities.Validation;
import utilities.Prompt;

/**
 *
 * @author fangchengjia
 */
public class MembershipManagerBase extends javax.swing.JFrame{
    final int MEMBERSHIP_LENGTH = 2;
    final int CARD_NO_LENGTH = 14;
    final int CARD_NO_START = 1000;
    final int CARD_NO_END = 9999;
    private final DBConnector dbSession;
    private javax.swing.JTable membershipTable;
    
    public final String TABLE_MODEL[] = new String[] {
        Macro.MEMBERSHIP_CARD_NUMBER_L,
        Macro.MEMBERSHIP_NAME_L,
        Macro.MEMBERSHIP_FELLOWSHIP_GROUP_L,
        Macro.MEMBERSHIP_GENDER_L,
        Macro.MEMBERSHIP_DATE_OF_BIRTH_L,
        Macro.MEMBERSHIP_REGISTRATION_DATE_L,
        Macro.MEMBERSHIP_EXPIRY_DATE_L,
        Macro.MEMBERSHIP_CONTACT_ADDRESS_L,
        Macro.MEMBERSHIP_PHONE_HOME_L,
        Macro.MEMBERSHIP_PHONE_OFFICE_L,
        Macro.MEMBERSHIP_PHONE_MOBILE_L,
        Macro.MEMBERSHIP_EMAIL_L,
        Macro.MEMBERSHIP_PAYMENT_STATUS_L,
        Macro.MEMBERSHIP_ID_L
    };
    
    /**
     * Creates new form CategoryManager
     * @param dbSession
     */
    public MembershipManagerBase(DBConnector dbSession) {
        this.dbSession = dbSession;
    }
    
    public DBConnector getDBSession(){
        return dbSession;
    }

    public javax.swing.JTable getTargetTable(){
        return membershipTable;
    }
    
    public void setTargetTable(javax.swing.JTable membershipTable){
        this.membershipTable = membershipTable;
    }
    
    public void refresh(){
        Helper.refresh(dbSession, Macro.DB_MEMBERSHIP, TABLE_MODEL,
                membershipTable);
    }
    
    String cardNumberGenerator(String gender, String group, String date){
        Random generator = new Random();
        String _fellowship = String.valueOf(gender.charAt(0));
        String _gender = String.valueOf(group.charAt(0));
        String _birthday = date.replaceAll("-", "");
        String card_no = "";
        do{
            String _random = String.valueOf(
                    generator.nextInt(CARD_NO_END-CARD_NO_START)+CARD_NO_START);
            card_no = String.format("%s%s%s%s",
                    _fellowship, _gender, _birthday, _random);
        }
        while(this.dbSession.check(Macro.DB_MEMBERSHIP,
                String.format("Card_No='%s'", card_no)));
        return card_no;
    }
    
    public void updateMember(Member member){
        if (inputCheck(member)){
            if (dbSession.check(Macro.DB_MEMBERSHIP, 
                    String.format("%s='%s'", Macro.MEMBERSHIP_ID_L, 
                        Codecs.encode(member.getId())))){
                dbSession.update(Macro.DB_MEMBERSHIP,
                        Helper.encodeValues(TABLE_MODEL, member.getValues()),
                        String.format("%s='%s'", Macro.MEMBERSHIP_ID_L, 
                            Codecs.encode(member.getId())));
                dispose();
            }
            else{
                Prompt.warning("The record does not exist.");
            }
        }
    }
    
    public boolean inputCheck(Member member){
        if (member.getName().isEmpty()){
            Prompt.warning(Macro.WARNING_EMPTY, "Name");
            return false;
        }
        
        if (member.getDateOfBirth().isEmpty()){
            Prompt.warning(Macro.WARNING_EMPTY, "Date of Birth");
            return false;
        }
        else{
            if (member.getDateOfBirth().matches(Macro.REGEX_DATE_SHORT) || 
                    member.getDateOfBirth().matches(Macro.REGEX_DATE)){
                if (member.getDateOfBirth().matches(Macro.REGEX_DATE_SHORT)){
                    member.setDateOfBirth(String.format("1900-%s",
                            member.getDateOfBirth()));
                }
                if (!Validation.checkDate(member.getDateOfBirth())){
                    Prompt.warning(Macro.WARNING_INVALID, "Date of Birth");
                    return false;
                }
            }
            else{
                Prompt.warning(Macro.WARNING_FORMAT, "Date of Birth",
                        "yyyy-mm-dd or mm-dd");
                return false;
            }
        }
        
        if (member.getRegistrationDate().isEmpty()){
            Prompt.warning(Macro.WARNING_EMPTY, "Registration Date");
            return false;
        }
        else{
            if (member.getRegistrationDate().matches(Macro.REGEX_DATE)){
                if(Validation.checkDate(member.getRegistrationDate())){
                    String year = member.getRegistrationDate().split("-")[0];
                    year = String.valueOf(Integer.parseInt(year) + MEMBERSHIP_LENGTH);
                    String expiry = member.getRegistrationDate().replaceFirst(
                            Macro.REGEX_YEAR, year);
                    member.setExpiryDate(expiry);
                }
                else{
                    Prompt.warning(Macro.WARNING_INVALID, "Registration Date");
                    return false;
                }
            }
            else{
                Prompt.warning(Macro.WARNING_FORMAT, "Registration Date",
                        "yyyy-mm-dd");
                return false;
            }
        }
        
        if (member.getPhoneHome().isEmpty() &&
                member.getPhoneOffice().isEmpty() && 
                member.getPhoneMobile().isEmpty() &&
                member.getEmail().isEmpty()){
            Prompt.warning("Please enter at least one contact method");
            return false;
        }
        else{
            if (!member.getPhoneHome().isEmpty()){
                if (!member.getPhoneHome().matches(Macro.REGEX_NUM)){
                    Prompt.warning("Phone (H) has to be all numbers");
                    return false;
                }
            }
            if (!member.getPhoneOffice().isEmpty()){
                if (!member.getPhoneOffice().matches(Macro.REGEX_NUM)){
                    Prompt.warning("Phone (O) has to be all numbers");
                    return false;
                }
            }
            if (!member.getPhoneMobile().isEmpty()){
                if (!member.getPhoneMobile().matches(Macro.REGEX_NUM)){
                    Prompt.warning("Phone (M) has to be all numbers");
                    return false;
                }
            }
            if (!member.getEmail().isEmpty()){
                if (!Pattern.compile(Macro.REGEX_EMAIL).matcher(
                        member.getEmail()).find()) {
                    Prompt.warning(Macro.WARNING_INVALID, "E-mail");
                    return false;
                }
            }
        }
        return true;
    }
}
